package com.ruoyi.zyx.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.ZyxDevice;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.sms.SmsUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zyx.domain.ZyxOrdershare;
import com.ruoyi.zyx.service.IZyxDeviceService;
import com.ruoyi.zyx.service.IZyxOrdershareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zyx.mapper.ZyxSendorderMapper;
import com.ruoyi.zyx.domain.ZyxSendorder;
import com.ruoyi.zyx.service.IZyxSendorderService;

/**
 * 派工Service业务层处理
 *
 * @author lhs
 * @date 2021-06-08
 */
@Service
public class ZyxSendorderServiceImpl implements IZyxSendorderService
{
    @Autowired
    private ZyxSendorderMapper zyxSendorderMapper;
    @Autowired
    private IZyxOrdershareService zyxOrdershareService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IZyxDeviceService zyxDeviceService;

    /**
     * 查询派工
     *
     * @param id 派工ID
     * @return 派工
     */
    @Override
    public ZyxSendorder selectZyxSendorderById(Long id)
    {
        return zyxSendorderMapper.selectZyxSendorderById(id);
    }

    /**
     * 查询派工列表
     *
     * @param zyxSendorder 派工
     * @return 派工
     */
    @Override
    @DataScope(deptAlias = "dd", userAlias = "")
    public List<ZyxSendorder> selectZyxSendorderList(ZyxSendorder zyxSendorder)
    {
        return zyxSendorderMapper.selectZyxSendorderList(zyxSendorder);
    }

    @Override
    @DataScope(deptAlias = "dd", userAlias = "")
    public List<ZyxSendorder> selectHomeZyxSendorderList(ZyxSendorder zyxSendorder) {
        return zyxSendorderMapper.selectHomeZyxSendorderList(zyxSendorder);
    }

    @DataScope(deptAlias = "dd", userAlias = "")
    public List<ZyxSendorder> selectNewZyxSendorder(ZyxSendorder zyxSendorder)
    {
        return zyxSendorderMapper.selectNewZyxSendorder(zyxSendorder);
    }


    @Override
    public List<ZyxSendorder> listMySendorder(ZyxSendorder zyxSendorder) {
        return zyxSendorderMapper.selectZyxSendorderList(zyxSendorder);
    }

    @Override
    public List<ZyxSendorder> listAllSendorder(ZyxSendorder zyxSendorder) {
        return zyxSendorderMapper.selectZyxSendorderList(zyxSendorder);
    }

    @Override
    public List<ZyxSendorder> listMyOwnSendorder(ZyxSendorder zyxSendorder) {
        return zyxSendorderMapper.selectZyxSendorderList(zyxSendorder);
    }

    /**
     * 新增派工
     *
     * @param zyxSendorder 派工
     * @return 结果
     */
    @Override
    public int insertZyxSendorder(ZyxSendorder zyxSendorder)
    {
        zyxSendorder.setCreateTime(DateUtils.getNowDate());
        //创建单号 派工类型+序号 序号：比如今天第三个派工：210623003
        //查询最新的派单生成单号
        ZyxSendorder zyxSendorderQuery=new ZyxSendorder();
        zyxSendorderQuery.setSendtype(zyxSendorder.getSendtype());
        zyxSendorderQuery.setSenddate(new Date());
        List<ZyxSendorder> zyxSendordernews=selectNewZyxSendorder( zyxSendorder);
        String orderno="";
        if(zyxSendordernews.size()>0){
            String oldno=zyxSendordernews.get(0).getOrderno();

            int newno=Integer.parseInt(oldno.substring(oldno.length()-3,oldno.length()))+1;
            orderno=oldno.substring(0,oldno.length()-3)+ String.format("%03d", newno);

        }else{
            String dt=DateUtils.dateTime();
            switch (zyxSendorder.getSendtype()){
                case "1":
                    orderno="WX"+dt+"001";
                    break;
                case "2":
                    orderno="BY"+dt+"001";
                    break;
                case "3":
                    orderno="JD"+dt+"001";
                    break;
                default:
                    orderno="YY"+dt+"001";
            }
        }
        zyxSendorder.setOrderno(orderno);

        int rows=zyxSendorderMapper.insertZyxSendorder(zyxSendorder);
        updateShare(zyxSendorder);
        return rows;
    }

    /**
     * 接单
     * @param zyxSendorder
     * @return
     */
    @Override
    public int takeOrder(ZyxSendorder zyxSendorder) {
        zyxSendorder.setServicestatus("2");
        zyxSendorder.setTakedate(new Date());
        int rows=zyxSendorderMapper.takeOrder(zyxSendorder);
        return rows;
    }

    @Override
    public int onlyupd(ZyxSendorder zyxSendorder) {
        int rows=zyxSendorderMapper.updateZyxSendorder(zyxSendorder);
        return rows;
    }

    /**
     * 服务
     * @param zyxSendorder
     * @return
     */
    @Override
    public int serviceOrder(ZyxSendorder zyxSendorder) {
        zyxSendorder.setServicestatus("3");
        zyxSendorder.setServicedate(new Date());
        int rows=zyxSendorderMapper.serviceOrder(zyxSendorder);
        ZyxSendorder zyxSendorderQ=zyxSendorderMapper.selectZyxSendorderById(zyxSendorder.getId());
        //发送到达短信
        ZyxOrdershare zyxOrdershare=new ZyxOrdershare();
        zyxOrdershare.setSendorderid(zyxSendorder.getId());
        List<ZyxOrdershare> zyxOrdershares=zyxOrdershareService.selectZyxOrdershareList(zyxOrdershare);
        for (ZyxOrdershare ordershare : zyxOrdershares) {
            try {
                //发送短信通知共享人 派工后工程师到达
                SysUser sysUser=sysUserService.selectUserById(ordershare.getUserid());
                SmsUtils.sendSms(sysUser.getUserName(),zyxSendorderQ.getMembername(),"SMS_218891085");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rows;
    }

    /**
     * 完成
     * @param zyxSendorder
     * @return
     */
    @Override
    public int overOrder(ZyxSendorder zyxSendorder) {
        ZyxSendorder zyxSendorderquery=zyxSendorderMapper.selectZyxSendorderById(zyxSendorder.getId());
        if(zyxSendorderquery.getSendtype().equals("1")){//维修
            ZyxDevice zyxDevice=new ZyxDevice();
            zyxDevice.setDevicestatus("1");
            zyxDevice.setId(zyxSendorder.getDeviceid());
            zyxDeviceService.updateZyxDevice(zyxDevice);
        }
        Double pr=0.0;
        try {
            pr=DateUtils.getDateHours(zyxSendorderquery.getServicedate(),new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        zyxSendorder.setProcessdate(pr);
        zyxSendorder.setServicestatus("4");
        zyxSendorder.setOverdate(new Date());

        int rows=zyxSendorderMapper.overOrder(zyxSendorder);
        //更新设备状态为正常

        //发送完成短信
        ZyxOrdershare zyxOrdershare=new ZyxOrdershare();
        zyxOrdershare.setSendorderid(zyxSendorder.getId());
        List<ZyxOrdershare> zyxOrdershares=zyxOrdershareService.selectZyxOrdershareList(zyxOrdershare);
        for (ZyxOrdershare ordershare : zyxOrdershares) {
            try {
                //发送短信通知共享人 派工后工程师到达
                SysUser sysUser=sysUserService.selectUserById(ordershare.getUserid());
                SmsUtils.sendSms(sysUser.getUserName(),zyxSendorderquery.getMembername(),"SMS_218172615");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rows;
    }

    public  int updateShare(ZyxSendorder zyxSendorder){
        ZyxSendorder zyxSendorderquery=zyxSendorderMapper.selectZyxSendorderById(zyxSendorder.getId());

        ZyxDevice zyxDevice=zyxDeviceService.selectZyxDeviceById(zyxSendorder.getDeviceid());
        zyxOrdershareService.delByOrderId(zyxSendorder.getId());
        if(null!=zyxSendorder.getIsshare()&&zyxSendorder.getIsshare().equals("Y")){
            for (Long seladmin : zyxSendorder.getSeladmins()) {
                ZyxOrdershare zyxOrdershare=new ZyxOrdershare();
                zyxOrdershare.setSendorderid(zyxSendorder.getId());
                zyxOrdershare.setUserid(seladmin);
                zyxOrdershareService.insertZyxOrdershare(zyxOrdershare);
                //发送短信通知共享人 派工短信
                SysUser sysUser=sysUserService.selectUserById(seladmin);
                try {
                    SmsUtils.sendSms(sysUser.getUserName(),zyxDevice.getDevicemodel(),"SMS_218157626");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }
    /**
     * 修改派工
     *
     * @param zyxSendorder 派工
     * @return 结果
     */
    @Override
    public int updateZyxSendorder(ZyxSendorder zyxSendorder)
    {
        zyxSendorder.setUpdateTime(DateUtils.getNowDate());
        int rows=zyxSendorderMapper.updateZyxSendorder(zyxSendorder);
        updateShare(zyxSendorder);
        return rows;
    }

    /**
     * 批量删除派工
     *
     * @param ids 需要删除的派工ID
     * @return 结果
     */
    @Override
    public int deleteZyxSendorderByIds(Long[] ids)
    {
        return zyxSendorderMapper.deleteZyxSendorderByIds(ids);
    }

    /**
     * 删除派工信息
     *
     * @param id 派工ID
     * @return 结果
     */
    @Override
    public int deleteZyxSendorderById(Long id)
    {
        return zyxSendorderMapper.deleteZyxSendorderById(id);
    }
}
